package com.eboss.sbs.activity;

import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;

import com.eboss.sbs.R;
import com.eboss.sbs.activity.base.BaseActivity;
import com.eboss.sbs.adapter.AccountInfoListAdapter;
import com.eboss.sbs.enums.EnumConsts;
import com.eboss.sbs.http.API;
import com.eboss.sbs.http.HttpCallback;
import com.eboss.sbs.model.AccountInfoEntity;
import com.eboss.sbs.model.AccountInfoListEntity;
import com.google.gson.reflect.TypeToken;
import com.mytools.tool.utils.LocalParasUtils;
import com.mytools.tool.views.MyListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class AccountInfoActivity extends BaseActivity {

	private MyListView listview;
	private AccountInfoListAdapter adapter;
	private List<AccountInfoEntity> datas;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		mActivity = this;
		setContentView(R.layout.activity_account_info);
		super.onCreate(savedInstanceState);
		initView();
		
	}
	
	private void initView(){
		showView(R.id.titleback_btn_back, true);
		setTvText(R.id.tv_title, "收款账户信息 ");
		
		datas = new ArrayList<AccountInfoEntity>();
		listview = (MyListView)findViewById(R.id.listview);
		adapter = new AccountInfoListAdapter(this, datas);
		listview.setAdapter(adapter);
		
		findViewById(R.id.titleback_btn_back).setOnClickListener(this);
		
		getdata();
	}
	
	private void getdata(){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sid", mApp.getLoginEntity().getSid());
		//wcp-2016-11-30
		TelephonyManager tm = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE); 
		String imei = tm.getDeviceId();
		params.put("serialNum", imei);
		API.http_post(this, API.ACCOUNT_INFO, params, new HttpCallback<AccountInfoListEntity>(this) {
			@Override
			public void onSuccess(int statusCode, AccountInfoListEntity data) {
				if(data!=null){
					setTvText(R.id.tv_member_name, data.getMemberName());
					if(data.getScanPayType()== EnumConsts.ScanPayType.SQB.getType()){
						showView(R.id.ly_sqb_account_infos, true);
						showView(R.id.ly_fuyou_account_infos, false);
						setTvText(R.id.tv_terminal_no, LocalParasUtils.getActivateCode(mActivity));
						setTvText(R.id.tv_terminal_name, LocalParasUtils.getTerminalName(mActivity));
						List<AccountInfoEntity> tmp_datas = data.getAccounts();
						if(tmp_datas!=null&&tmp_datas.size()>0){datas.addAll(tmp_datas);}
						datas.clear();
						adapter.notifyDataSetChanged();
					}
					else if(data.getScanPayType()== EnumConsts.ScanPayType.FUYOU.getType()){
						showView(R.id.ly_fuyou_account_infos, true);
						showView(R.id.ly_sqb_account_infos, false);
						setTvText(R.id.tv_fy_merchant_no, data.getFyMerchantNo());
						setTvText(R.id.tv_fy_merchant_name, data.getFyMerchantName());
					}
				}
			}
			@SuppressWarnings({ "unchecked", "rawtypes" })
			@Override
			public TypeToken getTypeToken() {
				return new TypeToken<AccountInfoListEntity>() {};
			}
		});
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.titleback_btn_back:
				finish();
				break;
	
			default:break;
		}
		super.onClick(v);
	}
	
}
